﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class cms_Admin_Users_u_Thongtincanhan : System.Web.UI.UserControl
{
    string uID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] != null) { uID = Session["uid"].ToString(); }
        if (!IsPostBack) { Viewinformation(); Showthutu(); }
    }
    #region[capnhat]
    protected void lbtCapnhat_Click(object sender, EventArgs e)
    {
        string img_path = "";
        if (fileupload.PostedFile != null && fileupload.PostedFile.ContentLength > 0)
        {
            img_path = System.IO.Path.GetFileName(fileupload.PostedFile.FileName);
            fileupload.PostedFile.SaveAs(Server.MapPath("Users/files/") + img_path.ToString().Trim());
            img_path = "Users/files/" + img_path.ToString().Trim();
        }
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByID(uID);
        if (list.Count > 0)
        {
            list[0].uID = uID;
            list[0].dpID = list[0].dpID;
            list[0].psID = list[0].psID;
            list[0].uUser = list[0].uUser;
            if (txtPassID.Text != "")
            {
                list[0].uPass = vmmsclass.Encodingvmms.Encode(txtPassID.Text);
            }
            else
            {
                list[0].uPass = list[0].uPass;
            }
            list[0].uName = txtTen.Text;
            list[0].uDate = txtNgaysinh.Text;
            list[0].uAdd = txtDiachi.Text;
            list[0].uTel = txtDienthoai.Text;
            list[0].uLevel = list[0].uLevel;
            list[0].uOrd = drlThutu.SelectedValue;
            list[0].uEmail = txtEmail.Text;
            list[0].uAdmin = list[0].uAdmin;
            list[0].uImg = img_path;
            try
            {
                if (tbUserDB.tbUsers_Update(list[0]))
                {
                    lblLoi.Text = "Cập nhật thông tin thành công !!";
                    Viewinformation();
                }
            }
            catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
        }
    }
    #endregion
    #region[Showthutu]
    void Showthutu()
    {
        drlThutu.Items.Clear();
        for (int i = 1; i < 501; i++)
        {
            drlThutu.Items.Add(i.ToString());
        }
    }
    #endregion
    #region[Viewinformation]
    void Viewinformation()
    {
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByID(uID);
        lblPhongban.Text = list[0].dpName;
        lblChucvu.Text = list[0].psName;
        lblAcount.Text = list[0].uUser;
        txtTen.Text = list[0].uName;
        lblMathanhvien.Text = list[0].uCode;
        txtNgaysinh.Text = list[0].uDate;
        txtDienthoai.Text = list[0].uTel;
        txtDiachi.Text = list[0].uAdd;
        txtEmail.Text = list[0].uEmail;
        if (list[0].uImg.Length > 0)
        {
            lblImg.Text = list[0].uImg.ToString();
            ltrImg.Text = Hinhanh_resize(list[0].uImg.ToString());
        }
        drlThutu.SelectedValue = list[0].uOrd;
    }
    #endregion
    #region[Hinhanh_resize]
    protected string Hinhanh_resize(string imgpath)
    {
        string chuoi = "";
        if (imgpath.ToString().Trim().Length > 0)
        {
            try
            {
                if (imgpath.IndexOf(".swf") > 0)
                {
                    chuoi += "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' border='0' width='150px' height='90'>\n";
                    chuoi += "<param name='movie' value='" + imgpath + "'>\n";
                    chuoi += "<param name='AllowScriptAccess' value='always'>\n";
                    chuoi += "<param name='quality' value='High'>\n";
                    chuoi += "<embed src='" + imgpath + "'\n";
                    chuoi += "pluginspage='http://www.macromedia.com/go/getflashplayer'\n";
                    chuoi += "type='application/x-shockwave-flash'\n";
                    chuoi += "allowscriptaccess='always'\n";
                    chuoi += "width='150'\n";
                    chuoi += "height='90'>\n";
                    chuoi += "</object>\n";
                }
                else
                {
                    System.Drawing.Image imgPhotoVert = System.Drawing.Image.FromFile(Server.MapPath(imgpath.ToString()));
                    int sourceWidth = imgPhotoVert.Width;
                    int sourceHeight = imgPhotoVert.Height;

                    float nPercent = 0;
                    float nPercentW = 0;
                    float nPercentH = 0;
                    if (sourceWidth > 178 || sourceHeight > 230)
                    {
                        nPercentW = ((float)178 / (float)sourceWidth);
                        nPercentH = ((float)230 / (float)sourceHeight);
                        if (nPercentH < nPercentW)
                        {
                            nPercent = nPercentH;
                        }
                        else
                        {
                            nPercent = nPercentW;
                        }
                        int destWidth = (int)(sourceWidth * nPercent);
                        int destHeight = (int)(sourceHeight * nPercent);
                        chuoi = "<img src=" + imgpath.ToString().Trim() + " border=0 width=" + destWidth + " height=" + destHeight + ">";

                    }
                    else
                    {
                        int destWidth = (int)(sourceWidth);
                        int destHeight = (int)(sourceHeight);
                        chuoi = "<img src=" + imgpath.ToString().Trim() + " border=0 width=" + destWidth + " height=" + destHeight + ">";

                    }
                }
            }
            catch { }
        }
        return chuoi;
    }
    #endregion
}
